package com.fare.taluo.core.domain.queue.message;

import java.util.List;

import javax.persistence.Table;

import org.apache.commons.lang.time.DateUtils;
import org.hibernate.annotations.Where;

import com.dayatang.domain.EntityRepository;
import com.dayatang.domain.InstanceFactory;
import com.dayatang.domain.QuerySettings;
import com.fare.taluo.core.domain.queue.QueueBaseEntity;
import com.fare.taluo.core.domain.queue.QueueBaseRepository;
import com.fare.taluo.core.domain.queue.message.repository.QueueWarnMessageRepository;
import com.fare.taluo.core.domain.queue.message.repository.impl.QueueWarnMessageRepositoryImpl;
import com.foreveross.threadpool.queue.WorkQueue;

@javax.persistence.Entity
@Table(name = "que_warn_message")
@Where(clause = "flg_deleted=0")
public class QueueWarnMessageEntity extends
		QueueBaseEntity<QueueWarnMessageEntity> implements
		com.dayatang.domain.Entity {

	/**
	 * 取任务
	 * 
	 * @return
	 * @throws Exception
	 */
	public QueueWarnMessageEntity doGet(Long id) throws Exception {
		return getEntityRepository().load(this.getClass(), id);
	}

	@Override
	protected QueueBaseRepository<QueueWarnMessageEntity> getEntityRepository() {
		return InstanceFactory
				.getInstance(QueueWarnMessageRepositoryImpl.class);
	}

	@Override
	public QueueWarnMessageEntity createNextTask(Long id) throws Exception {
		QueueWarnMessageEntity entity = this.doGet(id);
		QueueWarnMessageEntity newEntity = new QueueWarnMessageEntity();
		newEntity.setName(entity.getName());
		newEntity.setStatus(WorkQueue.STATUS_WAITING);
		newEntity.setCreator(entity.getCreator());
		newEntity.setCreatorName(entity.getCreatorName());
		newEntity.setPlanStartTime(DateUtils.addHours(entity.getPlanStartTime(), 1));
		newEntity.setFlgDeleted(0);
		// newEntity.setExecuteTimes(entity.getExecuteTimes());
		// 一个周期是一天
		// 这个时间，是否要取得配置项，再计算时间？
		// 按一天为周期，设置时间
//		newEntity.setGrapFromDate(org.apache.commons.lang3.time.DateUtils
//				.addDays(entity.getGrapFromDate(), 1));
//		newEntity.setGrapToDate(org.apache.commons.lang3.time.DateUtils
//				.addDays(entity.getGrapToDate(), 1));
//		newEntity.setPlanStartTime(org.apache.commons.lang3.time.DateUtils
//				.addDays(entity.getPlanStartTime(), 1));
//		newEntity.setExecuteTimes(0);
		this.getRepository().save(newEntity);
		return newEntity;
	}
	private static EntityRepository repository;
	public static EntityRepository getRepository() {
		if (repository == null) {
			repository = InstanceFactory.getInstance(QueueWarnMessageRepository.class);
		}
		return repository;
	}
	
	@Override
	public void executeTask(Long id) throws Exception {

	}

	@Override
	public boolean isNew() {
		return getId() == null || getId().intValue() == 0;
	}

	@Override
	public boolean existed() {
		if (isNew()) {
			return false;
		}
		return getRepository().exists(getClass(), id);
	}

	@Override
	public boolean notExisted() {
		return ! existed();
	}
	
	@Override
	public boolean existed(String propertyName, Object propertyValue) {
		List<?> entities = getRepository().find(QuerySettings.create(getClass()).eq(propertyName, propertyValue)); 
		return !(entities.isEmpty());
	}

}
